package srx.open.com.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobPointer;
import cn.bmob.v3.listener.FindListener;
import srx.open.com.R;
import srx.open.com.adapter.MyCourseAdpter;
import srx.open.com.bean.CoursePublish;
import srx.open.com.bean.SrxApplication;
import srx.open.com.bean.User;
import srx.open.com.utils.DataBaseHelper;
import srx.open.com.utils.Log;
import srx.open.com.utils.UserUtils;
import srx.open.com.views.RefreshListView;

/**
 * 我的课程界面.
 * 
 * @author hailongqiu 356752238@qq.com
 *
 */
public class MyCourseActivity extends ExtendBaseActivity {
	private static final String TAG = "MyCourseActivity";

	private static final int DOWN_PULL_STATE_UPDAE = 10000;
	private static final int UP_PULL_STATE_UPDATE = 10001;
	private static final int LIMIT_PAGE_NUM = 10;

	private int updateState = DOWN_PULL_STATE_UPDAE;
	private int mSkipPageNum = 0;
	private MyCourseAdpter mMyCourseAdpter;
	private DataBaseHelper mDataBaseHelper; // 数据库操作.
	private List<CoursePublish> mPubList = new ArrayList<CoursePublish>();

	@Bind(R.id.right_tv)
	TextView right_tv;
	@Bind(R.id.left_tv)
	TextView left_tv;
	@Bind(R.id.pull_refresh_list)
	RefreshListView mPullRefreshListView;

	@Override
	public void initAllDatas() {
		SrxApplication mSrxApplication = (SrxApplication) getApplication();
		/* 获取 数据库 DataBaseHelper */
		mDataBaseHelper = mSrxApplication.mDataBaseHelper;
	}

	@Override
	public void initAllViews() {
		setContentView(R.layout.activity_my_course);
		ButterKnife.bind(this);
		left_tv.setText("我的课程");
		right_tv.setText("发布课程");
//		initPullRefreshListView();
	}

	/*
	 * 初始化pullRefresh.
	 */
	private void initPullRefreshListView() {
		final User user = UserUtils.getCurrentUser(mContext);
		//
		mMyCourseAdpter = new MyCourseAdpter(mContext, mPubList,
				mDataBaseHelper);
		mPullRefreshListView.setAdapter(mMyCourseAdpter);
		// 连接item单击事件.
		mPullRefreshListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
			}
		});
//		// 下拉刷新数据.
//		mPullRefreshListView.setOnRefreshListener(new OnRefreshListener2() {
//			@Override
//			public void onPullDownToRefresh(PullToRefreshBase refreshView) {
//				updateState = DOWN_PULL_STATE_UPDAE;
//				queryUserPubCourse(user);
//			}
//
//			@Override
//			public void onPullUpToRefresh(PullToRefreshBase refreshView) {
//				updateState = UP_PULL_STATE_UPDATE;
//				queryUserPubCourse(user);
//			}
//		});
		if (user != null)
			queryUserPubCourse(user);
	}

	public void onQueryUserPCSuccess(final List<CoursePublish> coursePublishList) {
		Log.d("获取" + "课程成功:" + coursePublishList.size());
		if (coursePublishList.size() > 0) {
			if (updateState == DOWN_PULL_STATE_UPDAE) {
				mPubList = coursePublishList;
			} else {
				mPubList.addAll(coursePublishList);
			}
			mMyCourseAdpter.setPubList(mPubList);
		}
	}

	public void onQueryUserPCFailure(int code, String msg) {
		Log.d("获取" + "课程失败" + code + " " + msg);
	}

	@OnClick(R.id.left_lay)
	void left_lay(View view) {
		finish();
	}

	/*
	 * 发布课程.
	 */
	@OnClick(R.id.right_tv)
	void right_tv(View view) {
		Intent intents = new Intent(mContext, PublishCourseActivity.class);
		startActivity(intents);
	}

	/**
	 * 查询某用户发布的课程.
	 */
	public void queryUserPubCourse(final User queryUser) {
		if (updateState == DOWN_PULL_STATE_UPDAE) {
			mSkipPageNum = 0;
		} else {
			mSkipPageNum += LIMIT_PAGE_NUM;
		}
		BmobQuery<CoursePublish> publishss = new BmobQuery<CoursePublish>();
		/**
		 * 注意这里的查询条件 第一个参数：是User表中的publishss字段名
		 * 第二个参数：是指向User表中的某个用户的BmobPointer对象
		 */
		publishss.addWhereRelatedTo("publishss", new BmobPointer(queryUser));
		publishss.setLimit(LIMIT_PAGE_NUM); // 获取最接近用户地点的10条数据 [分页].
		publishss.setSkip(mSkipPageNum); // 分页效果
		publishss.findObjects(mContext, new FindListener<CoursePublish>() {

			@Override
			public void onSuccess(List<CoursePublish> coursePublishList) {
				onQueryUserPCSuccess(coursePublishList);
				mPullRefreshListView.onRefreshComplete();
			}

			@Override
			public void onError(int code, String msg) {
				onQueryUserPCFailure(code, msg);
				mPullRefreshListView.onRefreshComplete();
			}
		});
	}

}
